package com.leyou.item.servcie.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.servcie.SpecGroupService;
import com.leyou.item.servcie.SpecParamService;
import com.leyou.pojo.dto.SpecGroupDto;
import com.leyou.pojo.dto.SpecParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {
    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecGroupDto> querySpecGroupById(Long categoryId) {
        //根据品牌id查询规格组
        List<SpecGroup> specGroupList = list(new QueryWrapper<SpecGroup>().eq("category_id", categoryId));
        //转换成Dto
        List<SpecGroupDto> list = specGroupList.stream().map(SpecGroupDto::new).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<SpecGroupDto> querySpecGroupAndParamById(Long categoryId) {
        //查询规格组
        List<SpecGroupDto> specGroupDtoList = querySpecGroupById(categoryId);

        //尽量不要在for循环中进行语句查询
//        for (SpecGroupDto specGroupDto : specGroupDtoList) {
//            List<SpecParam> specParamList = specParamService.list(new QueryWrapper<SpecParam>().eq("group_id", specGroupDto.getId()));
//            List<SpecParamDto> paramDtoList = specParamList.stream().map(SpecParamDto::new).collect(Collectors.toList());
//            specGroupDto.setList(paramDtoList);
//        }
        //根据分类id查询规格参数
        List<SpecParam> specParamList = specParamService.list(new QueryWrapper<SpecParam>().eq("category_id", categoryId));
        //转换成Dto
        List<SpecParamDto> specParamDtoList = specParamList.stream().map(SpecParamDto::new).collect(Collectors.toList());
        //找出其中groupId相同的参数
        //根据groupId对specParamList进行分组
        Map<Long, List<SpecParamDto>> listMap = specParamDtoList.stream().collect(Collectors.groupingBy(SpecParamDto::getGroupId));
        //遍历集合，如果map中包含groupId，则赋值
        for (SpecGroupDto specGroupDto : specGroupDtoList) {
         if(listMap.containsKey( specGroupDto.getId()))  {
             List<SpecParamDto> specParamDtoList1 = listMap.get(specGroupDto.getId());
             specGroupDto.setList(specParamDtoList1);
         }
        }
        return specGroupDtoList;
    }

    @Override
    @Transactional
    public void saveGroup( SpecGroupDto specGroupDto) {
        //新增规格组只需要增加规格组一个表即可
        //转换成specGroup
        SpecGroup specGroup = specGroupDto.toEntity(SpecGroup.class);
        //新增规格组
        boolean save = save(specGroup);
        if(save==false){
            throw new LyException("新增规格组失败",500);
        }
    }

    @Override
    @Transactional
    public void updateGroup(SpecGroupDto specGroupDto) {
        //修改规格组只需要增加规格组一个表即可
        //转换成specGroup
        SpecGroup specGroup = specGroupDto.toEntity(SpecGroup.class);
        //修改规格组,id一定存在
        boolean b = updateById(specGroup);
        if(b==false){
            throw new LyException("修改规格组失败",500);
        }
    }

}
